﻿using AutoMapper;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.W;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.W
{
    /// <summary>
    /// 添加货运单
    /// </summary>
    public class CreatePendingShipmentWaybillsCommandHander : IRequestHandler<CreatePendingShipmentWaybillsCommand, ApiResult<int>>
    {
        /// <summary>
        /// 货运单
        /// </summary>
        private readonly IBaseRepository<PendingShipmentWaybillsModel> _pendingShipmentWaybillsModel;
        /// <summary>
        /// automapper转换
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="pendingShipmentWaybillsModel">货运单</param>
        public CreatePendingShipmentWaybillsCommandHander(IBaseRepository<PendingShipmentWaybillsModel> pendingShipmentWaybillsModel, IMapper mapper)
        {
            _pendingShipmentWaybillsModel = pendingShipmentWaybillsModel;
            this.mapper = mapper;
        }
        /// <summary>
        /// 添加货运单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public  Task<ApiResult<int>> Handle(CreatePendingShipmentWaybillsCommand request, CancellationToken cancellationToken)
        {
           ApiResult <int> res=new ApiResult<int>();
            var model = mapper.Map<PendingShipmentWaybillsModel>(request);
            int a =  _pendingShipmentWaybillsModel.Add(model);
            if (a > 0) 
            {
            res.Code=ApiEnums.Success;
                res.Msg = "添加成功";
                return Task.FromResult(res);
            }
            res.Code = ApiEnums.Fail;
            res.Msg = "添加失败";
            return Task.FromResult(res);
        }
    }
}
